<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>课程管理</el-breadcrumb-item>
      <el-breadcrumb-item>课程列表</el-breadcrumb-item>
      <el-breadcrumb-item>修改课程</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-card style="width: 600px;height: auto;margin: 0 auto;border: 1px solid black">
      <el-form :model="ruleForm">
        <el-form-item label="课程名称" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="所属系" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.dept"></el-input>
        </el-form-item>
        <el-form-item label="年级" :label-width="formLabelWidth">
          <el-select v-model="ruleForm.year">
            <el-option label="大一" value="大一"></el-option>
            <el-option label="大二" value="大二"></el-option>
            <el-option label="大三" value="大三"></el-option>
            <el-option label="大四" value="大四"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="教师" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.teacher"></el-input>
        </el-form-item>
        <el-form-item label="上课时间" :label-width="formLabelWidth">
          <el-select v-model="ruleForm.times">
            <el-option label="周一第一节" value="周一第一节"></el-option>
            <el-option label="周一第二节" value="周一第二节"></el-option>
            <el-option label="周一第三节" value="周一第三节"></el-option>
            <el-option label="周一第四节" value="周一第四节"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上课地点" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.loc"></el-input>
        </el-form-item>
        <el-form-item label="学分" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.credit"></el-input>
        </el-form-item>
        <el-form-item label="课程类型" :label-width="formLabelWidth">
          <el-select v-model="ruleForm.type">
            <el-option label="必修" value="必修"></el-option>
            <el-option label="选修" value="选修"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button style="margin: 30px 0 0 50px"
                     type="primary" @click="update()">确认修改
          </el-button>
          <el-button type="warning" @click="goBack()">返回</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        name:'', dept:'', year:'', teacher:'',
        times:'', loc:'', credit:'', type:''
      },
      formLabelWidth: '120px'
    };
  },
  methods: {
    goBack(){
      this.$router.push('/sys-admin/main/course/list');
    },
    update() {
      let url = 'http://localhost:9800/course/update' + location.search;
      console.log('url = ' + url);
      let formData = this.qs.stringify(this.ruleForm);
      console.log('formData = ' + formData);
      this.axios.post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '修改课程成功！',
            type: 'success'
          });
          this.$router.push('/sys-admin/main/course/list');
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    loadCourseDetail() {
      console.log('loadCourseDetail')
      let url = 'http://localhost:9800/course/selectById'+location.search;
      this.axios.get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.ruleForm = responseBody.data;
        } else {
          this.$message.error(responseBody.message);
        }
      })
    },
  },
  mounted() {
    this.loadCourseDetail();
  }
}
</script>